package gamebackend;

import framework.ImageWrapper;
import framework.ResourcesWrapper;

public class CloneShip extends AbstractShip {
	private static ImageWrapper my_image;
	
	private static final int MAX_SPECIAL = 16000;
	private static final int SPECIAL_COST = 4000;
	/**
	 * Whether this ship is controlled by the player or used for path generation.
	 */
	private final boolean am_player;
	/**
	 * Used for remembering where the next decoy should spawn.
	 */
	private int my_decoyIndex;
	private int my_special;
	public static void initialize(ResourcesWrapper the_resources) {
		my_image = the_resources.loadImage("ship.png");
	}

	public CloneShip(ShipListener the_listener, double the_x, double the_y) {
		super(the_listener, the_x, the_y);
		am_player = true;
	}
	private CloneShip(Ship the_ship) {
		super(the_ship);
		am_player = false;
	}
	@Override
	public void step(final int the_timeDelta, final double the_cameraYChange) {
		super.step(the_timeDelta, the_cameraYChange);
		if (am_player) {
			final int limit = (1 + getSpecialSegmentCount() - 2 * getDecoyCount()) * SPECIAL_COST;
			my_special += Math.min(the_timeDelta, Math.max(0, limit - my_special));
		}
	}

	@Override
	public int getSpecialSegmentCount() {
		// TODO Auto-generated method stub
		return MAX_SPECIAL / SPECIAL_COST;
	}

	@Override
	public double getSpecialPercent() {
		// TODO Auto-generated method stub
		return 100 * my_special / MAX_SPECIAL;
	}

	@Override
	public void special(boolean is_on) {
		if (is_on) {
			while (my_special >= SPECIAL_COST) {
				my_special -= SPECIAL_COST;
				makeNewDecoy();
			}
		}
		
	}

	@Override
	public Ship getCopy() {
		// TODO Auto-generated method stub
		return new CloneShip(this);
	}

	@Override
	protected ImageWrapper getImage() {
		// TODO Auto-generated method stub
		return my_image;
	}

	@Override
	protected double getSidewaysSpeed() {
		
		return  (getMinSpeed() - .1) * .3 + 0.21500082851239669 * .6;
	}

	@Override
	protected double getSidewaysAcceleration() {
		return getSidewaysSpeed() / 80;
	}

	@Override
	protected void onNewTier(int the_tier) {
		// TODO Auto-generated method stub
		if (am_player) {
			makeNewDecoy();
		} else {
			final ShipDecoy decoy = getNextDecoy();
			setXPosition(getXPosition() + decoy.getXPosition() * 2);
			setYPosition(getYPosition() + decoy.getYPosition() * 2);
		}
			
		
	}
	@Override
	public int getMaxHealth() {
		return 2;
	}
	private void makeNewDecoy() {
		addDecoy(getNextDecoy());
	}
	private ShipDecoy getNextDecoy() {
		final int totalDecoys = 7;
		final ShipDecoy decoy;
		switch (my_decoyIndex) {
			case 0:
				decoy = new ShipDecoy(25, -25);
				break;
			case 1:
				decoy = new ShipDecoy(-25, -25);
				break;
			case 2:
				decoy = new ShipDecoy(45, 0);
				break;
			case 3:
				decoy = new ShipDecoy(-45, 0);
				break;
			case 4:
				decoy = new ShipDecoy(13, -50);
				break;
			case 5:
				decoy = new ShipDecoy(-13, -50);
				break;
			default:
				decoy = new ShipDecoy(0, -75);
		}
		my_decoyIndex ++;
		if (my_decoyIndex >= totalDecoys) {
			my_decoyIndex = 0;
		}
		return decoy;
	}

}
